package com.gisuni.sddc.platformManager.controller;

import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import com.gisuni.sddc.platformManager.model.ResourceRLayer;
import com.gisuni.sddc.platformManager.model.Response;
import com.gisuni.sddc.platformManager.service.DataThemeService;


@IocBean(singleton = false)
@At("/client/backgroundManagement/DataThemeController")
public class DataThemeController  {
	private Log log = Logs.getLog(UserController.class);
	@Inject
    private DataThemeService dataThemeService;
	@At("/tree")
	@GET
	@POST
	@Ok("json")
	public Object allTreeNode() {
		return dataThemeService.allTreeNode();
	}
	
	@At("/addTreeNode")
	@GET
	@POST
	@Ok("json")
	public Object addTreeNode(@Param("name") String text, @Param("parentNodeId") String pid){
		Response response = new Response();
		try {
			dataThemeService.addTreeNode(text,pid);
			response.setSuccess(true);
			response.setMsg("添加成功");
			
		} catch (Exception e) {
			log.error(e);
			response.setSuccess(false);
			response.setMsg("添加失败");
		}
		return response;
	}
	
	@At("/delTreeNode")
	@GET
	@POST
	@Ok("json")
	public Object delTreeNode(@Param("id") String id) {
		Response response = new Response();
		try {
			dataThemeService.delTreeNode(id);
			response.setSuccess(true);
			response.setMsg("删除成功");
		} catch (Exception e) {
			log.error(e);
			response.setSuccess(false);
			response.setMsg("删除失败");
		}
		return response;
	}
	
	@At("/editTreeNode")
	@GET
	@POST
	@Ok("json")
	public Object editTreeNode(@Param("id") String id,@Param("text") String text) {
		Response response = new Response();
		response.setSuccess(false);
		try {
			dataThemeService.editTreeNode(id,text);
			response.setSuccess(true);
			response.setMsg("编辑成功");
		} catch (Exception e) {
			log.error(e);
			response.setMsg("编辑失败");
		}
		return response;
	}
	
	
	@At("/getConditionConfig")
	@GET
	@POST
	@Ok("json")
	public Object getConditionConfig(@Param("id") String id,@Param("pid") String pid) {
		Response response = new Response();
		response.setSuccess(false);
		String respResultString="";
		try {
			respResultString = dataThemeService.getConditionConfig(id,pid);
			response.setSuccess(true);
			response.setMsg("获取信息成功");
			response.setResult(respResultString);
		} catch (Exception e) {
			log.error(e);
			response.setMsg("获取信息失败");
		}
		return response;
	}
	
	@At("/getFieldValue")
	@GET
	@POST
	@Ok("json")
	public Object getFieldValue(@Param("id") String id,@Param("pid") String pid,@Param("field") String field) {
		Response response = new Response();
		response.setSuccess(false);
		String respResultString="";
		try {
			respResultString = dataThemeService.getFieldValue(id,pid,field);
			response.setSuccess(true);
			response.setMsg("获取信息成功");
			response.setResult(respResultString);
		} catch (Exception e) {
			log.error(e);
			response.setMsg("获取信息失败");
		}
		return response;
	}
	
	@At("/saveExpress")
	@GET
	@POST
	@Ok("json")
	public Object saveExpress(@Param("resourceId") String resourceId,@Param("layerId") String layerId,@Param("fields") String fields,@Param("edittable") int edittable,@Param("showfllevel") int showfllevel,@Param("tileFlag")int tileFlag) {
		Response response = new Response();
		response.setSuccess(false);
		try {
			if(dataThemeService.saveExpress(resourceId,layerId,fields,edittable,showfllevel,tileFlag)){
				response.setSuccess(true);
				response.setMsg("表达式已成功保存");
			}
		} catch (Exception e) {
			log.error(e);
			response.setMsg("表达式保存失败");
		}
		return response;
	}
	
	
	@At("/getDefinedExpress")
	@GET
	@POST
	@Ok("json")
	public Object getDefinedExpress(@Param("id") String id){
		Response response = new Response();
		response.setSuccess(false);
		try {
			ResourceRLayer resourceRLayer=dataThemeService.getDefinedExpress(id);
			if(resourceRLayer !=null){
				response.setSuccess(true);
				response.setMsg("找到关联信息");
				response.setResult(resourceRLayer);
			}else{
				response.setSuccess(true);
				response.setMsg("还没进行关联");
				response.setResult(resourceRLayer);
			}
		} catch (Exception e) {
			log.error(e);
			response.setMsg("获取信息失败");
		}
		return response;
	}
}